<?php

use App\Exceptions\ParamException;


/**
 * 抛出错误
 * @param $isErr
 * @param $msg
 * @throws Throwable
 */
function throw_err($isErr, $msg){
    throw_if($isErr, ParamException::class, $msg);
}

/**
 * 获取图片访问地址
 * @param $path
 * @return array|string
 */
function img_url($path)
{
    if (is_array($path)) {
        if (!$path) {
            return [];
        }
        foreach ($path as &$v) {
            if (strpos($v, 'https') === false && strpos($v, 'data:image') === false) {
                $v = env('ALIYUN_DOMAIN') . '/' . $v;
            }
        }
        return $path;
    }
    if (!$path) {
        return '';
    }
    if (strpos($path, 'https') === false && strpos($path, 'data:image') === false) {
        $path = env('ALIYUN_DOMAIN') . '/' . $path;
    }
    return $path;
}

/**
 * 距离格式化
 * @param $distance
 * @param $lat
 * @return string
 */
function distance_text($distance, $lat)
{
    if (!$lat) {
        return '未定位';
    }
    if ($distance <= 100) {
        return '100米内';
    }
    if ($distance < 1000) {
        return round($distance) . '米';
    }
    return round($distance / 1000, 1) . '公里';
}

/**
 * 默认坐标
 * @return array
 */
function default_location()
{
    $location = explode(',', env('LOCATION_DEFAULT'));
    return  [
        'lat' => $location[0],
        'lng' => $location[1],
    ];
}


